
/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();
var db = require('./db');

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

/*
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
*/

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var io = require('socket.io').listen(server);
io.on('connection', function(client){

  // sign in
  client.on('sign in', function(name){
    client.emit('del msg')
    var sql = "update t_user set presense=True, in_time=now() where name=" +
              "\'" + name + "\';";
    db.db(sql, function(){
      var sql = "select date_format(date, '%Y-%m-%d %k:%i:%s') as date," + 
                "name, val from t_test order by id desc limit 50";
      db.db(sql, function(rows){
        client.emit('msg push', rows);
      });
    });
    var sql = "select name from t_user where presense=True" + 
              " and (in_time > (now() - INTERVAL 360 second));";
    db.db(sql, function(user){
      client.emit('presense', user);
      client.broadcast.emit('presense', user);
    });
  });

  // submit
  client.on('msg send', function(msg, user){
    var sql = "insert into t_test(id, date, name, val) " +
              "values(default, now(), " +
              "\"" + user + "\"" + "," + "\"" + msg + "\"" +
              ");";
    db.db(sql, function(){
    client.emit('del msg');
    client.broadcast.emit('del msg');
    var sql = "select date_format(date, '%Y-%m-%d %k:%i:%s') as date," + 
              "name, val from t_test order by id desc limit 50";
      db.db(sql, function(rows){
        client.emit('msg push', rows);
        client.broadcast.emit('msg push', rows);
      });
    });
    var sql = "update t_user set presense=True, in_time=now() where name=" +
              "\'" + user + "\';";
    db.db(sql, function(){
    });
  });
  // sign out
  client.on('disconnect', function(user){
    /*
    var sql = "insert into t_test(id, date, name, val) " +
              "values(default, now(), " +
              "\"" + user + "\", " + "\"" + user + "sign out" + "\"" +
              ");";
    db.db(sql, function(rows){
      client.emit('del msg');
      client.broadcast.emit('del msg');
      var sql = "select date_format(date, '%Y-%m-%d %k:%i:%s') as date," + 
                "name, val from t_test order by id desc";
      db.db(sql, function(rows){
        client.emit('msg push', rows);
        client.broadcast.emit('msg push', rows);
        client.emit('del presense', user);
        client.broadcast.emit('del presense', user);
      });
    });
    */
  });
});
